package com.and.tdtwebapi;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.htg.tdtwebapi.TdtApiDrivingPlanning;
import com.htg.tdtwebapi.TdtApiGeocoder;
import com.htg.tdtwebapi.TdtApiRegion;
import com.htg.tdtwebapi.TdtApiSearch;
import com.htg.tdtwebapi.TdtApiTransitPlanning;
import com.orhanobut.logger.AndroidLogAdapter;
import com.orhanobut.logger.Logger;

import java.util.Arrays;
import java.util.List;

public class MainActivity extends Activity implements ExampleAdapter.OnItemClickListener {

    private RecyclerView recyclerView;

    private String key = "";

    private TdtApiSearch tdtApiSearch;
    private TdtApiTransitPlanning tdtApiTransitPlanning;
    private TdtApiGeocoder tdtApiGeocoder;
    private TdtApiRegion tdtApiRegion;
    private TdtApiDrivingPlanning tdtApiDrivingPlanning;


    private List<String> stringList = Arrays.asList("行政区划区域搜索服务", "视野内搜索", "周边搜索", "多边形搜索", "数据分类搜索", "普通搜索", "统计搜索", "公交规划",
            "公交线路信息", "公交站信息", "逆向线路信息", "地理编码", "逆向地理编码", "行政区划服务", "驾车规划");

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        recyclerView = findViewById(R.id.recyclerview);
        Logger.addLogAdapter(new AndroidLogAdapter());
        ExampleAdapter exampleAdapter = new ExampleAdapter(this, stringList);
        exampleAdapter.setOnItemClickListener(this);
        recyclerView.setAdapter(exampleAdapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));

        tdtApiSearch = new TdtApiSearch(key);
        tdtApiTransitPlanning = new TdtApiTransitPlanning(key);
        tdtApiGeocoder = new TdtApiGeocoder(key);
        tdtApiRegion = new TdtApiRegion(key);
        tdtApiDrivingPlanning = new TdtApiDrivingPlanning(key);
    }

    @Override
    public void onItemClick(int index, String name) {
        switch (index) {
            case 0:
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        String response = null;
                        try {
                            response = tdtApiSearch.searchRegion("商厦", "156110000", "12", "0", "2", "", "2");
                        } catch (Exception e) {
                            throw new RuntimeException(e);
                        }
                        Log.i("lala", "行政区划区域搜索服务 ： " + response);
                    }
                }).start();

                break;
            case 1:
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        String response = null;
                        try {
                            response = tdtApiSearch.searchInVision("医院", "116.02524,39.83833,116.65592,39.99185", "12", "2", "0", "2", "", "2");
                        } catch (Exception e) {
                            throw new RuntimeException(e);
                        }
                        Log.i("lala", "视野内搜索 ： " + response);
                    }
                }).start();
                break;
            case 2:
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        String response = null;
                        try {
                            response = tdtApiSearch.searchNear("公园", "5000", "116.48016,39.93136", "3", "0", "2", "", "2");
                        } catch (Exception e) {
                            throw new RuntimeException(e);
                        }
                        Log.i("lala", "周边搜索 ： " + response);
                    }
                }).start();
                break;
            case 3:
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        String response = null;
                        try {
                            response = tdtApiSearch.searchPolygon("学校", "118.93232636500011,27.423305726000024,118.93146426300007,27.30976105800005,118.80356153600007,27.311829507000027,118.80469010700006,27.311829508000073,118.8046900920001,27.32381604300008,118.77984777400002,27.32381601800006,118.77984779100007,27.312213007000025,118.76792266100006,27.31240586100006,118.76680145600005,27.429347074000077,118.93232636500011,27.423305726000024", "10", "0", "2", "", "2");
                        } catch (Exception e) {
                            throw new RuntimeException(e);
                        }
                        Log.i("lala", "多边形搜索 ： " + response);
                    }
                }).start();
                break;

            case 4:
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        String response = null;
                        try {
                            response = tdtApiSearch.searchDataClassification("156110000", "116.21,39.88,116.35,39.93", "13", "0", "2", "法院,公园", "2");
                        } catch (Exception e) {
                            throw new RuntimeException(e);
                        }
                        Log.i("lala", "数据分类搜索 ： " + response);
                    }
                }).start();
                break;
            case 5:
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        String response = null;
                        try {
                            response = tdtApiSearch.searchCommon("北京大学", "116.02524,39.83833,116.65592,39.99185", "12", "", "1", "0", "2", "", "2");
                        } catch (Exception e) {
                            throw new RuntimeException(e);
                        }
                        Log.i("lala", "普通搜索 ： " + response);
                    }
                }).start();
                break;
            case 6:
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        String response = null;
                        try {
                            response = tdtApiSearch.searchStatistics("学校", "156110108", "14", "", "2");
                        } catch (Exception e) {
                            throw new RuntimeException(e);
                        }
                        Log.i("lala", "统计搜索 ： " + response);
                    }
                }).start();
                break;

            case 7:
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        String response = null;
                        List<TdtApiTransitPlanning.Result> results;

                        try {
                            response = tdtApiTransitPlanning.planning("116.427562,39.939677", "116.349329,39.939132", "1");
                            results = tdtApiTransitPlanning.planning2("116.427562,39.939677", "116.349329,39.939132", "1");

                        } catch (Exception e) {
                            throw new RuntimeException(e);
                        }

                        Logger.json(response);
                        Logger.t("lala").json(new Gson().toJson(results));
                    }
                }).start();
                break;

            case 8:
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        String response = null;
                        try {
                            response = tdtApiTransitPlanning.busline("23212");
                            TdtApiTransitPlanning.BusLine busLine = tdtApiTransitPlanning.busline2("23212");
                            Logger.t("lala").json(new Gson().toJson(busLine));
                        } catch (Exception e) {
                            throw new RuntimeException(e);
                        }
                        //Logger.json(response);
                    }
                }).start();
                break;

            case 9:
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            TdtApiTransitPlanning.Station station = tdtApiTransitPlanning.station2("133057");
                            Logger.t("lala").json(new Gson().toJson(station));
                        } catch (Exception e) {
                            throw new RuntimeException(e);
                        }
                        //Logger.json(response);
                    }
                }).start();
                break;

            case 10:
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            //String response = tdtApiTransitPlanning.reverseBusLine("21169","128156");
                            //Logger.t("lala").json(response);
                            TdtApiTransitPlanning.BusLine busLine = tdtApiTransitPlanning.reverseBusLine2("21169", "128156");
                            Logger.t("lala").json(new Gson().toJson(busLine));
                        } catch (Exception e) {
                            throw new RuntimeException(e);
                        }
                        //Logger.json(response);
                    }
                }).start();
                break;
            case 11:
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            String response = tdtApiGeocoder.geocoder("北京市延庆区延庆镇莲花池村前街50夕阳红养老院");
                            Logger.t("lala").json(response);
                            TdtApiGeocoder.Location location = tdtApiGeocoder.geocoder2("北京市延庆区延庆镇莲花池村前街50夕阳红养老院");
                            Logger.t("lala").json(new Gson().toJson(location));
                        } catch (Exception e) {
                            throw new RuntimeException(e);
                        }
                    }
                }).start();
                break;
            case 12:
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            String response = tdtApiGeocoder.reGeocoder("116.37304", "39.92594", null);
                            Logger.t("lala").json(response);
                            TdtApiGeocoder.AddressComponent addressComponent = tdtApiGeocoder.reGeocoder2("116.37304", "39.92594", null);
                            Logger.t("lala").json(new Gson().toJson(addressComponent));
                        } catch (Exception e) {
                            throw new RuntimeException(e);
                        }
                    }
                }).start();
                break;
            case 13:
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            String response = tdtApiRegion.search("156120000", "3", true);
                            Logger.t("lala").json(response);
                            List<TdtApiRegion.Region> regions = tdtApiRegion.search2("156120000", "3", true);
                            Logger.t("lala").json(new Gson().toJson(regions));
                        } catch (Exception e) {
                            throw new RuntimeException(e);
                        }
                    }
                }).start();
                break;
            case 14:
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            //String response = tdtApiDrivingPlanning.plan("116.35506,39.92277","116.39751,39.90854",null,null);
                            //Logger.t("lala").xml(response);
                            TdtApiDrivingPlanning.Result result = tdtApiDrivingPlanning.plan2("116.35506,39.92277", "116.39751,39.90854", null, null);
                            Logger.t("lala").json(new Gson().toJson(result));
                        } catch (Exception e) {
                            throw new RuntimeException(e);
                        }
                    }
                }).start();
                break;
        }
    }
}